package org.ejs.file;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;

public final class FilesHandler {

  public FilesHandler() {
  }
  
  public void add(String file, byte[] content) throws IOException {
    Files.write(getPath(file), content, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
  }
  
  public void delete(String file) throws IOException {    
    Files.deleteIfExists(getPath(file));
  }
  
  protected String clearPath(String file) {
    return file.replaceAll("\\\\", File.separator).replaceAll("/", File.separator);
  }
  
  protected Path getPath(String file) {
    String realName = clearPath(file);
    String[] split = realName.split(File.separator);
    return Paths.get(split[0], Arrays.copyOfRange(split, 1, split.length));
  }

}
